<template>
  <div class="page-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>商铺信息</span>
        </div>
      </template>
      <el-form :model="form" :rules="rules" ref="merchantForm" label-width="100px" class="merchant-form">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="商铺名称" prop="shop_name">
              <el-input v-model="form.shop_name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="营业执照" prop="business_license">
              <el-input v-model="form.business_license" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="旧密码" prop="oldPassword">
              <el-input v-model="form.oldPassword" type="password" show-password></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="form.newPassword" type="password" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="确认新密码" prop="confirmPassword">
              <el-input v-model="form.confirmPassword" type="password" show-password></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserInfoStore } from '@/stores/userInfo'
import { getMerchantById, updateMerchantInfoService, resetMerchantPasswordService } from '@/api/merchant'

const userInfoStore = useUserInfoStore()
const merchantForm = ref(null)

const form = reactive({
  shop_name: '',
  business_license: '',
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const rules = {
  shop_name: [{ required: true, message: '请输入商铺名称', trigger: 'blur' }],
  oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const fetchMerchantInfo = async () => {
  try {
    const res = await getMerchantById(userInfoStore.userInfo.id)
    console.log(res)
    form.shop_name = res.data.data.shopName
    form.business_license = res.data.data.businessLicense
  } catch (error) {
    ElMessage.error('获取商铺信息失败')
  }
}

const handleSubmit = () => {
  merchantForm.value.validate(async (valid) => {
    if (valid) {
      try {
        await updateMerchantInfoService(userInfoStore.userInfo.id, form.shop_name)
        if (form.newPassword) {
          await resetMerchantPasswordService( userInfoStore.userInfo.id, form.oldPassword, form.newPassword)
        }
        ElMessage.success('更新成功')
        userInfoStore.setUserInfo({ ...userInfoStore.userInfo, shop_name: form.shop_name })
      } catch (error) {
        ElMessage.error('更新失败')
      }
    }
  })
}

fetchMerchantInfo()
</script>

<style lang="scss" scoped>
.page-container {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: hidden;
}

.box-card {
  width: 100%;
  max-width: 700px;
  margin: 0;
  padding: 12px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  height: 100%;
  display: flex;
  flex-direction: column;
}

.card-header {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-color);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-color);
}

.merchant-form {
  padding: 12px;
  flex: 1;
  overflow: hidden;

  .el-row {
    margin-bottom: 12px;
  }

  .el-form-item {
    margin-bottom: 18px;
  }
}
</style>